﻿
//
// Copyright (c) 2024 BPer
//
#include <bperavutil/audio/BPerAudioUtil.h>

#if BPer_OS_ANDROID
#include <bperaudiodev/android/BPerAudioCaptureAndroid.h>

void g_bPer_androidaudio_bqRecorderCallback(uint8_t* data,int32_t nb,void* user)
{

	if(user==NULL) return;

	BPerAudioCaptureHandle* ah=(BPerAudioCaptureHandle*)user;

	ah->putBuffer2(data, nb);

}
BPerAudioCaptureAndroid::BPerAudioCaptureAndroid(BPerAVInfo* avinfo) //:BPerAudioCapture(pcontext)
		{

	//m_context = pcontext;
    m_ahandle = new BPerAudioCaptureHandle(avinfo);
	aIndex = 0;
	m_size = 0;
	m_loops = 0;
	m_channel =avinfo->audio.channel;
	m_sample = avinfo->audio.sample;

	m_audioAndroid=(BPerAudioAndroid*)bPer_calloc(sizeof(BPerAudioAndroid),1);

}

BPerAudioCaptureAndroid::~BPerAudioCaptureAndroid() {
	/*if (m_loops) {
		stop();
		while (m_isStart) {
			bPer_usleep(500);
		}
	}*/
	closeAudio();
}
void BPerAudioCaptureAndroid::closeAudio() {

	bPer_destroy_audioAndroid(m_audioAndroid);
	bPer_free(m_audioAndroid);

}
void BPerAudioCaptureAndroid::setCatureStart() {
	m_ahandle->m_enableBuf = 1;
}
void BPerAudioCaptureAndroid::setCatureStop() {
	m_ahandle->m_enableBuf = 0;
}
void BPerAudioCaptureAndroid::setOutAudioBuffer(BPerAudioBuffer *pbuffer) {
	m_ahandle->setOutAudioBuffer(pbuffer);
}
void BPerAudioCaptureAndroid::setPlayAudoBuffer(BPerAudioBuffer *pbuffer) {
	m_ahandle->m_aecPlayBuffer = pbuffer;
}
void BPerAudioCaptureAndroid::setAec(BPerRtcAec *paec) {
	m_ahandle->m_aec = paec;
}
void BPerAudioCaptureAndroid::setInAudioBuffer(vector<BPerAudioPlayBuffer*> *pal) {

}
void BPerAudioCaptureAndroid::setPreProcess(BPerPreProcess *pp) {

}


int32_t BPerAudioCaptureAndroid::init() {
	if(bPer_create_audioAndroid_record(m_audioAndroid,g_bPer_androidaudio_bqRecorderCallback,m_ahandle, m_sample,m_channel)!=BPer_Ok){
		return bPer_error_wrap(ERROR_SYS_AudioRender,"init android record fail");
	}

	return BPer_Ok;
}

void BPerAudioCaptureAndroid::startLoop() {

	m_loops = 1;

	m_loops = 0;

}

void BPerAudioCaptureAndroid::stopLoop() {
	m_loops = 0;
}
#endif
